;
(function($, window, document, undefined) {
	function LazyLoad(eles, opt) {
		this.$eles = eles;
	}

	//节流函数
	function throttle(method, context) {
		clearTimeout(method.tId); //清除执行函数的定时器，第一次进入为空
		//设置一个100ms后在指定上下文中执行传入的method方法的定时器。
		method.tId = setTimeout(function() {
			method.call(context);
		}, 100);
	}
	//判断元素是否在可视区域外
	function isNotVisual($el) {
		var winScrollTop = $(window).scrollTop(),
			winHeight = $(window).height(),
			offsetTop = $el.offset().top,
			elHeight = $el.outerHeight();

		return winScrollTop > (offsetTop + elHeight) || winScrollTop < (offsetTop - winHeight)

	}

	LazyLoad.prototype.init = function() {
		var _self = this;

		_self.$eles.data('has-load', false);
		var doLazyShow = function() {
				var $el = null;
				//遍历每个元素
				_self.$eles.each(function(index, el) {
					$el = $(el);
					if (!$el.data('has-load') && !isNotVisual($el)) {
						$el.attr('src', $el.attr('data-lazysrc')).data('has-load', true);
					}
				});
			}
			//设置监听
		$(window).scroll(function(event) {
			throttle(doLazyShow, this);
		});

		doLazyShow();
		return _self.$eles;
	};


	$.fn.lazyLoad = function(opt) {
		var lz = new LazyLoad(this, opt);
		return lz.init();
	}
})(jQuery, window, document, undefined)